package com.thzy.bbs.thread;

import com.alibaba.fastjson.JSON;
import com.thzy.bbs.common.AddressConfig;
import com.thzy.bbs.common.CommonUtils;
import com.thzy.bbs.common.Constants;
import com.thzy.bbs.common.HttpRequestUtils;
import com.thzy.bbs.entity.FrontResult;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 上传图片子线程
 *
 * @author
 * @date 2017/1/5 17:25
 */

public class PhotoUploadingThread implements Runnable {
    private List<PhotoInfo> photoInfo;
    private String url;
    private String userId;

    public PhotoUploadingThread(List<PhotoInfo> photoInfo, String url, String userId) {
        this.photoInfo = photoInfo;
        this.url = url;
        this.userId = userId;
    }

    @Override
    public void run() {
        Map<String, Object> photosFile = getFiles();
        HttpRequestUtils.sendAsynchronous(HttpMethod.POST, url, null, photosFile,
                0, true, new Callback.ProgressCallback<String>() {

                    @Override
                    public void onSuccess(String result) {
                        CommonUtils.webView.post(new WebViewLoadThread(CommonUtils.webView, Constants.JsFunctionName.PHOTO_FUNCTION_NAME, result));
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        CommonUtils.webView.post(new WebViewLoadThread(CommonUtils.webView, Constants.JsFunctionName.PHOTO_FUNCTION_NAME,
                                JSON.toJSONString(new FrontResult(false, Constants.Code.UPLOAD_ERROR))));
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onWaiting() {

                    }

                    @Override
                    public void onStarted() {

                    }

                    @Override
                    public void onLoading(long total, long current, boolean isDownloading) {
                        if (total == 0) {
                            return;
                        }
                        Double percentage = (double) current / (double) total;
                        DecimalFormat df = new DecimalFormat("######0.00");
                        CommonUtils.webView.post(new WebViewLoadThread(CommonUtils.webView, Constants.JsFunctionName.PERCENTAGE, String.valueOf(df.format(percentage))));
                    }
                });
    }

    /**
     * 组装数据
     *
     * @return
     */
    private Map<String, Object> getFiles() {
        List<File> files = new ArrayList<>(photoInfo.size());
        for (PhotoInfo photoInfo : this.photoInfo) {
            files.add(new File(photoInfo.getPhotoPath()));
        }
        Map<String, Object> photosFile = new HashMap<>(1);
        if (url.equals(AddressConfig.ConfigAddress.IMAGE_HAND_URL)) {
            photosFile.put("userId", userId);
            photosFile.put("img", files.get(0));
        } else {
            photosFile.put("imgs", files);
        }
        return photosFile;
    }
}
